package com.Babysleep.Interface;

import android.content.Intent;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;

import com.Babysleep.R;
import com.Babysleep.Activity.BasicActivity;
import com.Babysleep.Activity.DanQu;
import com.Babysleep.Activity.MaMaShuo;
import com.Babysleep.Activity.NullActivity;
import com.Babysleep.Activity.YaoLanQu;

public class MyGesture implements OnGestureListener {
	private BasicActivity activity;
	private Intent intent = new Intent();
	private Intent[] intents;

	// 用户轻触触摸屏，由1个MotionEvent ACTION_DOWN触发

	public MyGesture(BasicActivity activity) {

		super();
		intents = new Intent[5];
		Intent intent = new Intent();

		intent = new Intent(activity, YaoLanQu.class);
		intent.putExtra("name", 0);
		intents[0] = intent;

		intent = new Intent(activity, DanQu.class);
		intent.putExtra("string_id", R.string.hailang);
		intent.putExtra("name", 1);
		intents[1] = intent;

		intent = new Intent(activity, DanQu.class);
		intent.putExtra("string_id", R.string.yudi);
		intent.putExtra("name", 2);
		intents[2] = intent;

		intent = new Intent(activity, DanQu.class);
		intent.putExtra("string_id", R.string.baizaoyin);
		intent.putExtra("name", 3);
		intents[3] = intent;

		intent = new Intent(activity, MaMaShuo.class);
		intent.putExtra("name", 4);
		intents[4] = intent;

		this.activity = activity;
	}

	public boolean onDown(MotionEvent e) {
		//ViewFlipper flipper=(ViewFlipper)activity.findViewById(R.id.flipper);
		//flipper.setInAnimation(AnimationUtils.loadAnimation(activity, android.R.anim.fade_in));
		//flipper.setOutAnimation(AnimationUtils.loadAnimation(activity, android.R.anim.fade_out));
		//flipper.showNext();
		return true;
	}

	// 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN,
	// 多个ACTION_MOVE, 1个ACTION_UP触发
	// e1：第1个ACTION_DOWN MotionEvent
	// e2：最后一个ACTION_MOVE MotionEvent
	// velocityX：X轴上的移动速度，像素/秒
	// velocityY：Y轴上的移动速度，像素/秒
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if(activity.player.isPlaying())
		activity.player.Stop();
		activity.player.release();
		System.out.println("ok");
		System.out.println(e1.getX() - e2.getX());
		if (e1.getX() - e2.getX() > 0) {
			if (activity.name == 4) {
				intent=new Intent(activity, NullActivity.class);
				intent.putExtra("name", 4);
				activity.startActivity(intent);
				activity.overridePendingTransition(R.anim.donghua2_left_in,
						R.anim.donghua2_left_out);
			} else {
				System.out.println(activity.name+"bug");
				activity.startActivity(intents[activity.name + 1]);
				activity.overridePendingTransition(R.anim.push_left_in,
						R.anim.push_left_out);
			}

		}
		if (e1.getX() - e2.getX() < 0) {
			if (activity.name == 0) {
				intent=new Intent(activity, NullActivity.class);
				intent.putExtra("name", 0);
				activity.startActivity(intent);
				activity.overridePendingTransition(R.anim.donghua_right_in,
						R.anim.donghua_right_out);
			} else {
				activity.startActivity(intents[activity.name - 1]);
				activity.overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
				
			}

		}

		return true;
	}
	
	
	// 用户长按触摸屏，由多个MotionEvent ACTION_DOWN触发
	public void onLongPress(MotionEvent e) {
		Log.d("TAG", "[onLongPress]");
	}

	// 用户按下触摸屏，并拖动，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		activity.findViewById(R.id.back_to_list).setVisibility(View.INVISIBLE);
		Log.d("TAG", "[onScroll]");
		return true;
	}

	// 用户轻触触摸屏，尚未松开或拖动，由一个1个MotionEvent ACTION_DOWN触发
	// 注意和onDown()的区别，强调的是没有松开或者拖动的状态
	public void onShowPress(MotionEvent e) {
		Log.d("TAG", "[onShowPress]");

	}

	// 用户（轻触触摸屏后）松开，由一个1个MotionEvent ACTION_UP触发
	public boolean onSingleTapUp(MotionEvent e) {
		Log.d("TAG", "[onSingleTapUp]");
		return true;
	}
}
